package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.mes.basicdata.dao.iface.ITypesDataDao;
import com.mes.basicdata.domain.TypesData;
import com.mes.check.domain.CheckTask;
import com.mes.common.dao.BasicDao;

@Repository
public class TypesDataDao extends BasicDao<TypesData> implements ITypesDataDao {

	@Override
	public List<TypesData> getTypesDatasByTypeName(Map<String, Object> params) {
		String hql = "from TypesData where typeName = :typeName order by length(id),id";
		List<TypesData> list = this.query(hql, params);
		return list;
	}

	@Override
	public String getTypeIdByTypeName(String typeName, String idDesc) {
		String id = "";
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = "select typeId from TypesData where typeName = ? and idDescription = ?";
		Query query = session.createQuery(hql);
		query.setString(0, typeName);
		query.setString(1, idDesc);
		Object obj = query.uniqueResult();
		if (obj != null)
			id = (String) obj;
		return id;
	}
	
	@Override
	public String getTypeIdDescByTypeName(String typeName, String typeId) {
		String idDesc = "";
		Session session = this.getSessionFactory().getCurrentSession();
		String hql = "select idDescription from TypesData where typeName = ? and typeId = ?";
		Query query = session.createQuery(hql);
		query.setString(0, typeName);
		query.setString(1, typeId);
		Object obj = query.uniqueResult();
		if (obj != null)
			idDesc = (String) obj;
		return idDesc;
	}

	@Override
	public List<TypesData> getTypeNames() {
		String hql = "select distinct typeName from TypesData";
		List<String> typeNames = this.query(hql);

		List<TypesData> typesDatas = new ArrayList<TypesData>();
		for (String typeName : typeNames) {
			TypesData typesData = new TypesData();
			typesData.setTypeName(typeName);
			typesDatas.add(typesData);
		}

		return typesDatas;
	}

}
